package com.kevin.Code.DP;

/**
 * @author Vinlee Xiao
 * @Classname DeleteOperationForTwoStrings
 * @Description Leetcode 583. 两个字符串的删除操作 思路同Leetcode 72
 * @Date 2021/10/5 10:38
 * @Version 1.0
 */
public class DeleteOperationForTwoStrings {
    public static void main(String[] args) {

    }

    /**
     * @param word1
     * @param word2
     * @return
     */
    public int minDistance(String word1, String word2) {

        int m = word1.length();
        int n = word2.length();

        if (m == 0 || n == 0) {

            return m == 0 ? n : m;
        }

        int[][] dp = new int[m + 1][n + 1];

        //当word2为空时,word2删除
        for (int i = 0; i < m + 1; i++) {
            dp[i][0] = i;
        }

        for (int i = 0; i < n + 1; i++) {
            dp[0][i] = i;
        }

        for (int i = 1; i < m + 1; i++) {

            for (int j = 1; j < n + 1; j++) {

                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    //不相同时删除
                    dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + 1;
                }

            }
        }

        return dp[m][n];

    }
}
